package com.tyf.common.redis;

import com.tyf.common.log.LogUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @Title <p>
 *        Description:redis操作工具类
 *        </P>
 * @author tan.bin
 * @date 2016年10月10日
 *
 */
@SuppressWarnings("unchecked")
@Component
public class RedisManager {
	@SuppressWarnings("rawtypes")
	@Autowired
	RedisTemplate redisTemplate;
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	/**
	 * 批量删除key
	 * 
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys.size() > 0)
			redisTemplate.delete(keys);
	}

	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		Object result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate
				.opsForValue();
		result = operations.get(key);
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate
					.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 从左边将元素压入list
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean leftPush(final String key, Object value){
		try{
			ListOperations<Serializable, Object> listOps = redisTemplate.opsForList();
			listOps.leftPush(key, value);
			
//			redisTemplate.boundListOps(key).leftPush(value);
		} catch (Exception e){
			LogUtils.logInfo(logger, "", e);
			return false;
		}
		return true;
	}

	/**
	 * 将list的中的元素从右边弹出
	 * @param key
	 * @return
	 */
	public Object rightPop(final String key){
		try{
			ListOperations<Serializable, Object> listOps = redisTemplate.opsForList();
			return listOps.rightPop(key);
		} catch (Exception e){
			LogUtils.logInfo(logger, "", e);
			return null;
		}
	}

	/**
	 * 获取hashmap结构的value
	 * @param key 主key
	 * @param hkey hashkey
	 * @return
	 */
	public Object hget(final String key, final String hkey){
		try{
			HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash();
			return hashOps.get(key, hkey);
		} catch (Exception e){
			LogUtils.logInfo(logger, "", e);
			return null;
		}
	}

	/**
	 * 设置hashmap结构的value
	 * @param key
	 * @param hkey
	 * @param hvalue
	 */
	public void hput(final String key, final String hkey, final Object hvalue){
		try{
			HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash();
			hashOps.put(key, hkey, hvalue);
		} catch (Exception e){
			LogUtils.logInfo(logger, "", e);
		}
	}

	/**
	 * 获取hashmap结构的所有值
	 * @param key
	 */
	public Map<String, Object> getEntries(final String key){
		try{
			HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash();
			return hashOps.entries(key);
		} catch (Exception e){
			LogUtils.logInfo(logger, "", e);
			return null;
		}
	}

	/**
	 * 设置有效期 , 时间为妙
	 * @param key
	 * @param expireTime
	 */
	public void setExpire(final String key, Long expireTime) {
		redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
	}

	@SuppressWarnings("rawtypes")
	public RedisTemplate getRedisTemplate() {
		return redisTemplate;
	}

	@SuppressWarnings("rawtypes")
	public void setRedisTemplate(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

}
